﻿using System;
using System.Data;
using System.Data.SqlClient;
using Entities;

namespace DAL
{
    public class DAL_Faqs
    {
        private const string usp_SelectFaqs      = "usp_SelectFaqs";
        private const string usp_SelectFaqByID   = "usp_SelectFaqByID";
        private const string usp_SelectFaqTop    = "usp_SelectFaqTop";
        private const string usp_InsertFaqs      = "usp_InsertFaq";
        private const string usp_UpdateFaqs      = "usp_UpdateFaq";
        private const string usp_DeleteFaqs      = "usp_DeleteFaq";
        private const string usp_SelectFaqDetail = "usp_SelectFaqDetail";

        private const Int32  OK                  = 1;
        private const Int32  NOT_OK              = -1;

        public DataTable Get_Faqs_All()
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sqlDataAdapter;
            SqlCommand sqlCommand = new SqlCommand(usp_SelectFaqs, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                Utils.sqlConnection.Open();
                sqlDataAdapter.Fill(dt);
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return dt;
            }
            return dt;
        }

        public DataTable Get_Faqs_Top()
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sqlDataAdapter;
            SqlCommand sqlCommand = new SqlCommand(usp_SelectFaqTop, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                Utils.sqlConnection.Open();
                sqlDataAdapter.Fill(dt);
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return dt;
            }
            return dt;
        }

        public DataTable Get_Faqs_ByID(int p_faq_id)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sqlDataAdapter;
            SqlParameter[] sqlParameter = new SqlParameter[1];
            SqlCommand sqlCommand = new SqlCommand(usp_SelectFaqByID, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].SqlDbType = SqlDbType.BigInt;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@Faq_id";
            sqlParameter[0].Value = p_faq_id;

            sqlCommand.Parameters.AddRange(sqlParameter);
            sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                Utils.sqlConnection.Open();
                sqlDataAdapter.Fill(dt);
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return dt;
            }
            return dt;
        }

        public DataSet Get_FaqDetail(int p_faq_id)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter sqlDataAdapter;
            SqlParameter[] sqlParameter = new SqlParameter[1];
            SqlCommand sqlCommand = new SqlCommand(usp_SelectFaqDetail, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].SqlDbType = SqlDbType.BigInt;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@Faq_id";
            sqlParameter[0].Value = p_faq_id;

            sqlCommand.Parameters.AddRange(sqlParameter);
            sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                Utils.sqlConnection.Open();
                sqlDataAdapter.Fill(ds);
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return ds;
            }
            return ds;
        }

        public bool DeleteFaq(int p_faq_id)
        {
            SqlParameter[] sqlParameter = new SqlParameter[1];
            SqlCommand sqlCommand = new SqlCommand(usp_DeleteFaqs, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].SqlDbType = SqlDbType.BigInt;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@Faq_id";
            sqlParameter[0].Value = p_faq_id;
            sqlCommand.Parameters.AddRange(sqlParameter);
            try
            {
                Utils.sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return false;
            }
            return true;
        }

        public Int32 InsertFaq(Faqs pFaqs)
        {
            Int32 _iFaq_id = 0;

            SqlParameter[] sqlParameter = new SqlParameter[5];
            SqlCommand sqlCommand = new SqlCommand(usp_InsertFaqs, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].SqlDbType = SqlDbType.NVarChar;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@Title";
            sqlParameter[0].Value = pFaqs.TITLE;

            sqlParameter[1] = new SqlParameter();
            sqlParameter[1].SqlDbType = SqlDbType.NText;
            sqlParameter[1].Direction = ParameterDirection.Input;
            sqlParameter[1].ParameterName = "@Question";
            sqlParameter[1].Value = pFaqs.QUESTION;

            sqlParameter[2] = new SqlParameter();
            sqlParameter[2].SqlDbType = SqlDbType.NText;
            sqlParameter[2].Direction = ParameterDirection.Input;
            sqlParameter[2].ParameterName = "@Answer";
            sqlParameter[2].Value = pFaqs.ANSWER;

            sqlParameter[3] = new SqlParameter();
            sqlParameter[3].SqlDbType = SqlDbType.VarChar;
            sqlParameter[3].Direction = ParameterDirection.Input;
            sqlParameter[3].ParameterName = "@Email";
            sqlParameter[3].Value = pFaqs.EMAIL;            

            sqlParameter[4] = new SqlParameter();
            sqlParameter[4].SqlDbType = SqlDbType.Int;
            sqlParameter[4].Direction = ParameterDirection.Output;
            sqlParameter[4].ParameterName = "@Faq_id";

            sqlCommand.Parameters.AddRange(sqlParameter);
            try
            {
                Utils.sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
                Utils.sqlConnection.Close();
                _iFaq_id = Int32.Parse(sqlParameter[4].Value.ToString());
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return NOT_OK;
            }
            return _iFaq_id;
        }

        public bool UpdateFaq(Faqs pFaqs)
        {
            SqlParameter[] sqlParameter = new SqlParameter[5];
            SqlCommand sqlCommand = new SqlCommand(usp_UpdateFaqs, Utils.sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlParameter[0] = new SqlParameter();
            sqlParameter[0].SqlDbType = SqlDbType.NVarChar;
            sqlParameter[0].Direction = ParameterDirection.Input;
            sqlParameter[0].ParameterName = "@Title";
            sqlParameter[0].Value = pFaqs.TITLE;

            sqlParameter[1] = new SqlParameter();
            sqlParameter[1].SqlDbType = SqlDbType.NText;
            sqlParameter[1].Direction = ParameterDirection.Input;
            sqlParameter[1].ParameterName = "@Question";
            sqlParameter[1].Value = pFaqs.QUESTION;

            sqlParameter[2] = new SqlParameter();
            sqlParameter[2].SqlDbType = SqlDbType.NText;
            sqlParameter[2].Direction = ParameterDirection.Input;
            sqlParameter[2].ParameterName = "@Answer";
            sqlParameter[2].Value = pFaqs.ANSWER;

            sqlParameter[3] = new SqlParameter();
            sqlParameter[3].SqlDbType = SqlDbType.VarChar;
            sqlParameter[3].Direction = ParameterDirection.Input;
            sqlParameter[3].ParameterName = "@Email";
            sqlParameter[3].Value = pFaqs.EMAIL;

            sqlParameter[4] = new SqlParameter();
            sqlParameter[4].SqlDbType = SqlDbType.Int;
            sqlParameter[4].Direction = ParameterDirection.Input;
            sqlParameter[4].ParameterName = "@Faq_id";
            sqlParameter[4].Value = pFaqs.FAQ_ID;            

            sqlCommand.Parameters.AddRange(sqlParameter);
            try
            {
                Utils.sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
                Utils.sqlConnection.Close();
            }
            catch
            {
                if (Utils.sqlConnection.State == ConnectionState.Open)
                {
                    Utils.sqlConnection.Close();
                }
                return false;
            }
            return true;
        }
    }
}
